package com.itheima.app.manager.umsManager;

import cn.hutool.core.util.StrUtil;
import com.itheima.commons.R;
import com.itheima.domain.db.ums.UmsMenu;
import com.itheima.domain.db.ums.UmsRole;
import com.itheima.dto.UmsMenuDto;
import com.itheima.service.umsService.UmsMenuService;
import com.itheima.service.umsService.UmsRoleService;
import com.itheima.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Component
public class UmsMenuManager {


    @DubboReference
    UmsMenuService umsMenuService;


    //分页查询菜单数据
    public R findByPage(Integer pageNum, Integer pageSize, Long parentId) {
        //2.远程调用service,返回pageBeanVo
        PageBeanVo pageBeanVo = umsMenuService.findByPage(pageNum, pageSize, parentId);

        //6.返回pageBeanVo
        return R.ok(pageBeanVo);

    }

    /*
     *添加后台菜单
     * */
    public R addUsmMenu(UmsMenu umsMenu) {
        if (umsMenu != null) {
            umsMenu.setCreateTime(new Date());
            Long parentId = umsMenu.getParentId();
            if (parentId == 0) {
                umsMenu.setLevel(0);
            } else {
                umsMenu.setLevel(1);
            }
            umsMenuService.addUsmMenu(umsMenu);
            return R.ok("");
        }
        return  R.err("添加失败");
    }

    /*
     *根据ID删除后台菜单
     * */
    public R deleteById(Long id) {
        //查询菜单信息
        UmsMenu umsMenu = umsMenuService.findById(id);

        if (umsMenu.getParentId()!= 0){
            umsMenuService.deleteByUsmMenuId(id);
            return R.ok("");
        }
        if (umsMenuService.findCount(id) ==0){
            umsMenuService.deleteByUsmMenuId(id);
            return R.ok("");
        }
        return R.err("有下级菜单，无法删除");
    }

    // 返回树状的菜单集合
    public R findMenuTreeList() {
        List<UmsMenuDto> List = umsMenuService.findMenuTreeList(0L);

        return R.ok(List);
    }

    /*
     *修改后台菜单
     * */
    public R updateById(Long id,UmsMenu umsMenu) {
        //根据id获取对象
        UmsMenu byId = umsMenuService.findById(id);

        if (byId != null){
            umsMenuService.updateByUmsMenu(umsMenu);
            return R.ok("");
        }
        return R.err("修改失败");
    }

    public R findMenuById(Long id) {
        UmsMenu byId = umsMenuService.findById(id);

        return R.ok(byId);
    }

    // 修改菜单显示状态
    public R updateMenuHidden(Long id, Integer hidden) {
        umsMenuService.updateMenuHidden(id, hidden);
        return R.ok("");
    }
}
